package com.example.administrator.basedemos.activity;

import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.administrator.basedemos.R;
import com.example.administrator.basedemos.adapter.MainAdapter;
import com.example.administrator.basedemos.api.GitHubAPi;
import com.example.administrator.basedemos.base.BaseActivity;
import com.example.administrator.basedemos.bean.GithubUserBean;
import com.example.administrator.basedemos.bean.UserBean;
import com.example.administrator.basedemos.retorfit.RetrofitCallBack;
import com.example.administrator.basedemos.utils.LogUtil;
import com.example.administrator.basedemos.utils.TmtUtils;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 创建者     ZCL
 * 创建时间   2018/3/12 17:36
 * 描述	      ${TODO}
 * <p>
 * 更新者     $Author$
 * 更新时间   $Date$
 * 更新描述   ${TODO}
 */

public class Retrofit2Activity extends BaseActivity {
    private static final String TAG     = "Retrofit2Activity";
    public static        String baseUrl = "https://api.github.com";
    @BindView(R.id.recyclerview_mm)
    RecyclerView mRecyclerviewMm;
    private GitHubAPi mAPiService;

    @Override
    public void initView() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        mAPiService = retrofit.create(GitHubAPi.class);
    }

    @Override
    public void initData() {
        ArrayList<String> list = new ArrayList<>();
        list.add("GET");
        list.add("GET_PATH");
        list.add("GET_PARAMERS");
        list.add("POST_FORM");
        list.add("POST_JSON");
        list.add("上传wenjian");
        list.add("rxlogin");
        list.add("rx登陸后獲取數據");

        mRecyclerviewMm.setLayoutManager(new StaggeredGridLayoutManager(4, OrientationHelper.HORIZONTAL));
        MainAdapter adapter = new MainAdapter(R.layout.main_item, list);
        mRecyclerviewMm.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (position) {
                    case 0:
                        apiGet();
                        break;
                    case 1:
                        apiGetPath();
                        break;
                    case 2:
                        apiGetParams();
                        break;
                    case 3:
                        apiPostForm();
                        break;
                    case 4:
                        apiPostJson();
                        break;
                    case 5:
                        apiUpLoad();
                        break;
                    case 6:
                        apiUpLoad();
                        break;
                    case 7:
                        rxLogin();
                        break;

                    default:
                        break;
                }
            }
        });


    }

    private void rxLogin() {
        mAPiService.rxLogin(new UserBean("zhang", "zcl"))
                .flatMap(new Function<GithubUserBean, ObservableSource<UserBean>>() {
                    @Override
                    public ObservableSource<UserBean> apply(GithubUserBean githubUserBean) throws Exception {
                        return mAPiService.rxToken(githubUserBean.token);
                    }
                }).subscribeOn(Schedulers.io())//请求数据在io线程里面
                .observeOn(AndroidSchedulers.mainThread())//让更新在主线程更新界面
                .doOnNext(new Consumer<UserBean>() {
                    @Override
                    public void accept(UserBean userBean) throws Exception {
                        //可以在現實之前進行操作比如講信息現實之前先把頭像下載下來
                    }
                    //1.0是action1  2.0是Consumer
                }).subscribe(new Consumer<UserBean>() {
            @Override
            public void accept(UserBean userBean) throws Exception {
                TmtUtils.midToast(Retrofit2Activity.this, "更新头像成功!");
            }
        });
    }

    private void apiUpLoad() {

    }

    private void apiPostJson() {

    }

    private void apiPostForm() {

        mAPiService.createUser(new UserBean("zhang", "zcl")).enqueue(new RetrofitCallBack(this) {

            @Override
            protected void setSuccessMessage(GithubUserBean body) {
                LogUtil.util(TAG, "返回数据是---------" + body);
            }
        });
    }

    private void apiGet() {
        mAPiService.githubUser().enqueue(new RetrofitCallBack(this) {
            @Override
            protected void setSuccessMessage(GithubUserBean body) {
                LogUtil.util(TAG, "返回数据是---------" + body);
            }
        });
    }

    private void apiGetPath() {
        mAPiService.githubUserOne("1").enqueue(new RetrofitCallBack(this) {
            @Override
            protected void setSuccessMessage(GithubUserBean body) {
                LogUtil.util(TAG, "返回数据是----------" + body.toString());
            }
        });
    }

    private void apiGetParams() {
        HashMap<String, String> map = new HashMap<>();
        map.put("id", "zcl");
        map.put("name", "zhang");
        mAPiService.githubUserParam(map).enqueue(new RetrofitCallBack(this) {
            @Override
            protected void setSuccessMessage(GithubUserBean body) {
                LogUtil.util(TAG, "返回数据是------------------------------:" + body.toString());
            }
        });
    }

    @Override
    public int setLayoutId() {
        return R.layout.activity_retrofit;
    }
}
